package com.fengwan.wastesort.widget

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.annotation.Nullable
import androidx.appcompat.widget.LinearLayoutCompat
import com.fengwan.wastesort.R
import kotlinx.android.synthetic.main.toolbar_comm.view.*

class ToolBarCommView : LinearLayoutCompat {
    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)


    init {
        inflate(context, R.layout.toolbar_comm, this)

        iv_title_left.setOnClickListener { onBack(it) }
    }

    private fun onBack(view: View) {
        if (view.context is Activity) {
            (view.context as Activity).onBackPressed()
        }
    }

    fun setIvRight(resId: Int, onClickListener: OnClickListener? = null): ToolBarCommView {
        iv_title_right.apply {
            setImageResource(resId)
            visibility = View.VISIBLE
            setOnClickListener(onClickListener)
        }
        return this
    }

    fun setIvRight(resId: Int, listener: (view: View) -> Unit): ToolBarCommView {
        iv_title_right.apply {
            setImageResource(resId)
            visibility = View.VISIBLE
            setOnClickListener(listener)
        }
        return this
    }

    fun showBack(isShow: Boolean): ToolBarCommView {
        if (!isShow) iv_title_left.visibility = View.GONE else iv_title_left.visibility = View.VISIBLE
        return this
    }

    fun setTitle(title: CharSequence): ToolBarCommView {
        tv_title_center.text = title
        return this
    }

    fun setTitleLeft(title: CharSequence, onClickListener: OnClickListener? = null): ToolBarCommView {
        tv_title_left.apply {
            text = title
            visibility = View.VISIBLE
            setOnClickListener(onClickListener)
        }
        return this
    }

    fun setTitleRight(title: CharSequence, onClickListener: OnClickListener? = null): ToolBarCommView {
        tv_title_right.apply {
            text = title
            visibility = View.VISIBLE
            setOnClickListener(onClickListener)
        }
        return this
    }


}